import { Upload } from 'antd';
import { LoadingOutlined, PlusOutlined } from '@ant-design/icons';
import React from 'react'

class UploadProductImg extends React.Component {
  state = {
    loading: false,
  };

  handleChange = info => {
    if (info.file.status === 'uploading') {
      this.setState({ loading: true });
      return;
    }
    if (info.file.status === 'done') {
      // Get this url from response in real world.
      const successImg = info.file.response.headimgurl
      /* antd中form表单提供的受控函数 */
      this.props.onChange(successImg)
      this.setState({
        imageUrl: successImg
      })
    }
  };

  render() {
    const { loading, imageUrl } = this.state;
    const imgUrlOr = imageUrl || this.props.value
    const uploadButton = (
      <div>
        {loading ? <LoadingOutlined /> : <PlusOutlined />}
        <div style={{ marginTop: 8 }}>Upload</div>
      </div>
    );
    return (
        <Upload
          name="headimgurl"
          listType="picture-card"
          className="avatar-uploader"
          showUploadList={false}
          action="/api/students/uploadStuAvatar"
          onChange={this.handleChange}
          
        >
          {imgUrlOr ? <img src={imgUrlOr} alt="avatar" style={{ width: '100%' }} /> : uploadButton}
        </Upload>
    );
  }
}

export default UploadProductImg